<?php
App::uses('AppController', 'Controller');
/**
 * Acos Controller
 *
 * @property Aco $Aco
 */
class AcosController extends AppController {

	public function beforeRender(){
		parent::beforeRender();
		$this->set('actions',array('create'=>__('Create',true),'read'=>__('Read',true),'update'=>__('Update',true),'delete'=>__('Delete',true),'*'=>__('All')));
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {
		if ($this->request->is('post')) {
			$parents = $this->Aco->getPath($this->request->data['Aco']['aco_id'],array('Aco.alias'));
			$i = '';
			$a = '';
			foreach ($parents as $key => $value) {
				$a .= $i.$value['Aco']['alias'];
				if($i == '') $i ='/';
			}
			$this->loadModel('Group');
			$this->Group->id = $this->request->data['Aco']['group_id'];
			if($this->request->data['Aco']['access']) $this->Acl->allow($this->Group, $a,$this->request->data['Aco']['action_id']);
			else $this->Acl->deny($this->Group, $a,$this->request->data['Aco']['action_id']);
			$this->redirect($this->referer());
		}
		$this->Aco->recursive = 0;
		$aco = $this->Aco->find('threaded');
		$acos = $this->treeForCombobox($aco,'Aco','alias',true);
		
		$this->loadModel('Group');
		$this->Group->recursive = 0;
		$groups = $this->Group->find('list');
		$this->set(compact('acos','groups'));
	}	

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Aco->id = $id;
		if (!$this->Aco->exists()) {
			throw new NotFoundException(__('Invalid aco'));
		}
		$this->set('aco', $this->Aco->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Aco->create();
			if ($this->Aco->save($this->request->data)) {
				$this->Session->setFlash(__('The aco has been saved'));
				
			} else {
				$this->Session->setFlash(__('The aco could not be saved. Please, try again.'));
			}
			$this->redirect(array('controller'=>'aros_acos','action' => 'index'));
		}
		$this->Aco->recursive = 0;
		$aco = $this->Aco->find('threaded');
		$acos = $this->treeForCombobox($aco,'Aco',array('model'=>'Aco','name'=>'alias'),false);
		
		$aros = $this->Aco->Aro->find('list');
		$this->set(compact('aros','acos'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Aco->id = $id;
		if (!$this->Aco->exists()) {
			throw new NotFoundException(__('Invalid aco'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Aco->save($this->request->data)) {
				$this->Session->setFlash(__('The aco has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The aco could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Aco->read(null, $id);
		}
		$aros = $this->Aco->Aro->find('list');
		$this->set(compact('aros'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Aco->id = $id;
		if (!$this->Aco->exists()) {
			throw new NotFoundException(__('Invalid aco'));
		}
		if ($this->Aco->delete()) {
			$this->Session->setFlash(__('Aco deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Aco was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
